﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using BaseHelper.Base;

namespace BaseHelper
{
    public class LogHelper
    {
        public static string LogPath;
        public static string ErrPath;
        public static string DebugPath;
        public static string AttackPath;
        public static string BaseFolder;
        public static bool IsDebugConsole = false;


        public static void SetFolder(string Folder=null)
        {
            if (Folder == null)
                Folder = SysHelper.AppPath+"日志记录";

            if (!Directory.Exists(Folder))
                Directory.CreateDirectory(Folder);

            BaseFolder = Folder;

            LogPath = Folder + "\\服务日志.txt";
            ErrPath = Folder + "\\报错日志.txt";
            DebugPath = Folder + "\\调试日志.txt";
            AttackPath = Folder + "\\攻击日志.txt";
            
        }

        public static void Service(string ServerName,string Context)
        {
            try
            {
                if (BaseFolder == null)
                    SetFolder();

                string line = string.Format("【{0}】【{1}】{2}\r\n", DateTime.Now.ToString(), ServerName, Context);


                if (IsDebugConsole)
                    Console.WriteLine(line);


                if (LogPath != null)
                    File.AppendAllText(LogPath, line, Encoding.Default);
            }
            catch { }

        }

        public static void Debug(string DebugName, string Context)
        {
            try
            {
                if (BaseFolder == null)
                    SetFolder();

                string line = string.Format("【{0}】【{1}】{2}\r\n", DateTime.Now.ToString(), DebugName, Context);

                if (IsDebugConsole)
                    Console.WriteLine(line);

                if (DebugPath != null)
                    File.AppendAllText(DebugPath, line, Encoding.Default);
            }
            catch { }

        }


        public static void Attack(string LocalIP,string RemoteIP,int Port)
        {
            try
            {
                if (BaseFolder == null)
                    SetFolder();

                string line = string.Format("【{0}】【端口{1}】本机IP：{2}  对方IP：{3}\r\n", DateTime.Now.ToString(), Port, LocalIP, RemoteIP);


                if (IsDebugConsole)
                    Console.WriteLine(line);


                if (AttackPath != null)
                    File.AppendAllText(AttackPath, line, Encoding.Default);
            }
            catch { }

        }


        public static void Err(string Class, string Function, Exception ex)      
        {
            try
            {
                if (BaseFolder == null)
                    SetFolder();

                string line = string.Format("{0}；{1}；{2}", ex.Message, ex.Source, ex.StackTrace);

                if (IsDebugConsole)
                    Console.WriteLine(line);


                Err(Class, Function, line);
            }
            catch (Exception exx)
            {
                Err("Log.cs", "Err()", exx);
            }
            
        }

        public static void Err(string Class, string Function, string Str)
        {
            try
            {
                if (BaseFolder == null)
                    SetFolder();

                string line = string.Format("【{0}】【ERROR::{1}->{2}】{3}\r\n---------------------------------------------\r\n", DateTime.Now.ToString(), Class, Function,Str);

                if (IsDebugConsole)
                    Console.WriteLine(line);


                if (ErrPath != null)
                    File.AppendAllText(ErrPath, line, Encoding.Default);
            }
            catch { }
        }

        

    }
}
